﻿//-----------------------------------------------------------------------
// <copyright file="IMergeCellInternal.cs" company="LMT">
//     Copyright (c) by La Minh Thuận.
// </copyright>
// <author>thuanla1985@hotmail.com</author>
//-----------------------------------------------------------------------
namespace LMT.Forms
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    /// <summary>
    /// A specify TextBoxColumn class has a merge ability
    /// </summary>
    [ToolboxBitmap(typeof(DataGridViewTextBoxColumn))]
    public class MergeGridViewTextBoxColumn : MergeGridViewColumn
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MergeGridViewTextBoxColumn"/> class.
        /// </summary>
        public MergeGridViewTextBoxColumn() : base(new MergeGridViewTextBoxCell())
        {
        }

        /// <summary>
        /// Gets or sets the template used to create new cells.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// A <see cref="T:System.Windows.Forms.DataGridViewCell"/> that all other cells in the column are modeled after. The default is null.
        /// </returns>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }

            set
            {
                if ((value != null) && !(value is MergeGridViewTextBoxCell))
                {
                    throw new InvalidCastException("This column can only contain MergeGridViewTextBoxCell.");
                }

                base.CellTemplate = value;
            }
        }

        /// <summary>
        /// Gets or sets the length of the max input.
        /// </summary>
        /// <value>The length of the max input.</value>
        [Category("Behavior"), Description("MergeGridViewTextBoxColumn max input length"), DefaultValue(0x7fff)]
        public int MaxInputLength
        {
            get
            {
                if (this.TextBoxCellTemplate == null)
                {
                    throw new InvalidOperationException("MergeGridViewColumn Cell template required");
                }

                return this.TextBoxCellTemplate.MaxInputLength;
            }

            set
            {
                if (this.MaxInputLength != value)
                {
                    this.TextBoxCellTemplate.MaxInputLength = value;
                    if (this.DataGridView != null)
                    {
                        DataGridViewRowCollection rows = this.DataGridView.Rows;
                        int count = rows.Count;
                        for (int i = 0; i < count; i++)
                        {
                            DataGridViewTextBoxCell cell = rows.SharedRow(i).Cells[this.Index] as DataGridViewTextBoxCell;
                            if (cell != null)
                            {
                                cell.MaxInputLength = value;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Gets the text box cell template.
        /// </summary>
        /// <value>The text box cell template.</value>
        private DataGridViewTextBoxCell TextBoxCellTemplate
        {
            get
            {
                return (DataGridViewTextBoxCell)this.CellTemplate;
            }
        }
    }
}
